package com.hzmall.controller;

import com.hzmall.common.Const;
import com.hzmall.common.ResponseCode;
import com.hzmall.common.ServerResponse;
import com.hzmall.dao.ShippingMapper;
import com.hzmall.pojo.Shipping;
import com.hzmall.pojo.User;
import com.hzmall.service.impl.ShippingService;
import com.hzmall.util.CheckLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.spi.ServiceRegistry;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/shipping/")
public class ShippingController {

    @Autowired
    private ShippingService shippingService;

    /**
     * 新增收货地址
     */
    @RequestMapping(value = "add.do")
    @ResponseBody
    public ServerResponse add(HttpServletRequest request, Shipping shipping){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.shippingService.add(currentUser.getId(), shipping);
    }

    /**
     * 删除地址
     */
    @RequestMapping(value = "del.do")
    @ResponseBody
    public ServerResponse del(HttpServletRequest request,Integer shippingId){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.shippingService.del(currentUser.getId(),shippingId);
    }

    /**
     * 登陆状态下更新收货地址
     */
    @RequestMapping(value = "update.do")
    @ResponseBody
    public ServerResponse update(HttpServletRequest request,Shipping shipping){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.shippingService.update(currentUser.getId(),shipping);
    }

    /**
     * 查看选中地址
     */
    @RequestMapping(value = "select.do")
    @ResponseBody
    public ServerResponse select(HttpServletRequest request,Integer shippingId){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.shippingService.select(currentUser.getId(),shippingId);
    }

    /**
     * 收货地址列表
     */
    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse list(HttpServletRequest request, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.shippingService.list(currentUser.getId(),pageNum,pageSize);
    }
}
